package com.example.beixiangsuapp.bxs.views;

import android.content.Context;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.beixiangsuapp.R;
import com.recker.flybanner.FlyBanner;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class PinnedHeaderExpandableAdapter extends BaseExpandableListAdapter implements PinnedHeaderExpandableListView.HeaderAdapter {
    Map<String, List<Object>> childrenData;
    // private String[] groupData;
    private Context context;
    private PinnedHeaderExpandableListView listView;
    private List<String> groups;
    private LayoutInflater inflater;
    private ArrayList images;

    public PinnedHeaderExpandableAdapter(Map<String, List<Object>> childrenData, String[] groupData
            , Context context, PinnedHeaderExpandableListView listView) {
        // this.groupData = groupData;
        this.childrenData = childrenData;
        this.context = context;
        this.listView = listView;
        inflater = LayoutInflater.from(this.context);
    }

    public PinnedHeaderExpandableAdapter(Map<String, List<Object>> childrenData, Context context, PinnedHeaderExpandableListView listView, List<String> groups, ArrayList images) {
        this.childrenData = childrenData;
        this.context = context;
        this.listView = listView;
        this.groups = groups;
        this.images = images;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<Object> objects = childrenData.get("first");
        Object o = objects.get(childPosition);
        return o;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        View view = createChildrenView();
        TextView text = view.findViewById(R.id.text);
        List<Object> objects = childrenData.get("first");
        Object o = objects.get(childPosition);
        text.setText("" + o);
        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupPosition >= 0) {
            List<Object> objects = childrenData.get("first");
            return objects.size();
        } else {
            return 0;
        }


    }

    @Override
    public Object getGroup(int groupPosition) {
        String n = "";
        if (groupPosition >= 0) {
            Set<String> strings = childrenData.keySet();
            Iterator<String> iterator = strings.iterator();
            while (iterator.hasNext()) {
                n = iterator.next();
            }
            return n;
        } else {
            return n;
        }

    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        convertView = null;
        convertView = createGroupView();
       /* FlyBanner fly_banner = convertView.findViewById(R.id.fly_banner);
        if (images.size() > 0) {
            fly_banner.setImagesUrl(images);
        }*/

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private View createChildrenView() {
        View v = inflater.inflate(R.layout.item_home, null);
        return v;
    }

    private View createGroupView() {
        View v = inflater.inflate(R.layout.head_view, null);
        return v;
    }


    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !listView.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition,
                                int childPosition, int alpha) {
    }

    private SparseIntArray groupStatusMap = new SparseIntArray();

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if (groupStatusMap.keyAt(groupPosition) >= 0) {
            return groupStatusMap.get(groupPosition);
        } else {
            return 0;
        }
    }
}
